<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php'); #加载全站初始化配置文件
require_once(dirname(__FILE__) . '/includes/Response.php');
require_once(dirname(__FILE__) . '/includes/Jpush.php');


//发送评论消息接口
if($_REQUEST['act'] == 'send') {
$_POST['sender_id'] = isset($_POST['sender_id']) ? trim($_POST['sender_id']) : '';
$_POST['receiver_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';
$_POST['message_text'] = isset($_POST['message_text']) ? trim($_POST['message_text']) : '';

//    $_POST['sender_id'] = 1;
//    $_POST['receiver_id'] = 2;
//    $_POST['message_text'] = 'sdfsdff';
    if (empty($_POST['message_text'])) {

        return Response::json(456, "消息内容不能为空");

    } else {

        $sql = "insert into " . $ecs->table('message_text') . " (sender_id,message_text) values('" . $_POST['sender_id'] . "','" . $_POST['message_text'] . "')";
        $row1 = $db->query($sql);
        $sql = "SELECT LAST_INSERT_ID()";
        $message_text_id = $db->getOne($sql);
        $send_time = date("Y-m-d H:i:s");
        $sql = "insert into " . $ecs->table('message') . " (sender_id,receiver_id,send_time,message_text_id) values('" . $_POST['sender_id'] . "','" . $_POST['receiver_id'] . "','". $send_time . "','" . $message_text_id . "')";
        $row2 = $db->query($sql);
        return Response::json(200, "消息发送成功");
    }

}

//未读消息接口
elseif($_REQUEST['act'] == 'unread_message') {
    $_POST['receiver_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';
//    $_POST['receiver_id'] = 2;

    $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['receiver_id'] . " AND read_flag = 0 ORDER BY send_time DESC";
    $unread0 = $db->getAll($sql);
    foreach ($unread0 as $value1){
        $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$value1['sender_id'];
        $sender_name = $db->getRow($sql);
        $row0 = array(
            'sender_name' => $sender_name,
            'message' => $value1
        );
        $arr0[] = $row0;
    }
    $count0 = count($unread0);
    $sql = "SELECT ecs_message.*,ecs_message_text.* FROM ".$ecs->table('message')."INNER JOIN " .$ecs->table('message_text')." ON ecs_message.message_text_id = ecs_message_text.message_text_id WHERE ecs_message.receiver_id = " .$_POST['receiver_id']. " AND ecs_message.read_flag = 0 ORDER BY send_time DESC";
    $unread1 = $db->getAll($sql);
    foreach ($unread1 as $value2) {
        $sql = "SELECT user_name FROM " . $ecs->table('user') . " WHERE user_id = " .$value2['sender_id'];
        $sender_name = $db->getRow($sql);
        $sql = "SELECT activity_name FROM " . $ecs->table('activity') . " WHERE activity_id = " .$value2['activity_id'];
        $activity_name = $db->getRow($sql);
        $row1 = array(
            'sender_name' => $sender_name,
            'activity_name' => $activity_name,
            'message' => $value2
        );
        $arr1[] = $row1;
    }
    $count1 = count($unread1);
    $sql = "SELECT * FROM ".$ecs->table('inform_message')." WHERE receiver_id = " .$_POST['receiver_id']. " AND read_flag = 0 ORDER BY send_time DESC";
    $unread2 = $db->getAll($sql);
    foreach ($unread2 as $value3) {
        $sql = "SELECT user_name FROM " . $ecs->table('user') . " WHERE user_id = " .$value3['sender_id'];
        $sender_name = $db->getRow($sql);
        $sql = "SELECT activity_name FROM " . $ecs->table('activity') . " WHERE activity_id = " .$value3['activity_id'];
        $activity_name = $db->getRow($sql);
        $row2 = array(
            'sender_name' => $sender_name,
            'activity_name' => $activity_name,
            'message' => $value3
        );
        $arr2 = $row2;
    }
    $count2 = count($unread2);
    $count = $count0 + $count1 + $count2;

    $unread = array(
        'unread_count' => $count,
        'hi_unread_count' => $count0,
        'hi_unread' => $arr0,
        'review_unread_count' => $count1,
        'review_unread' => $arr1,
        'inform_unread_count' => $count2,
        'inform_unread' => $arr2
    );

    return Response::json(200,'未读消息获取成功',$unread);

}

//所有消息接口
elseif($_REQUEST['act'] == 'message') {
    $_POST['receiver_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';

    //提取所有被回复的打招呼id
    $sql1 = "SELECT DISTINCT receive_message_id FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['receiver_id']." AND (receive_message_id IS NOT NULL)";
    $receive_message_id = $db->getAll($sql1);

    $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE ( receiver_id = " .$_POST['receiver_id']." AND receive_message_id IS NULL ) OR (id in ($sql1)) ORDER BY send_time";
    $first_hi_message = $db->getAll($sql);
    if(!empty($first_hi_message)) {
        foreach ($first_hi_message as $value){
            if($value['receiver_id'] == $value['receiver_id']){
                $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
                $sender_name =$db->getOne($sql);
                $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
                $receiver_name =$db->getOne($sql);
                $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE  receive_message_id = '".$value['id']."' ORDER BY send_time";
                $second_hi_message = $db->getAll($sql);
                foreach ($first_hi_message as $v){
                    $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
                    $sender_name =$db->getOne($sql);
                    $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
                    $receiver_name =$db->getOne($sql);

                    $res2 = array(
                        'sender_name' => $sender_name,
                        'receiver_name' => $receiver_name,
                        'message' => $v
                    );
                    $res1[] = $res2;

                }
                $res = array(
                    'type' => 1,
                    'sender_name' => $sender_name,
                    'receiver_name' => $receiver_name,
                    'message' => $value,
                    'second_hi_message' => $res1
                );
                $arr0[] = $res;

            }else{
                $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
                $sender_name =$db->getOne($sql);
                $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
                $receiver_name =$db->getOne($sql);
                $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE  receive_message_id = '".$value['id']."'ORDER BY send_time";
                $second_hi_message = $db->getAll($sql);
                foreach ($first_hi_message as $v){
                    $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
                    $sender_name =$db->getOne($sql);
                    $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
                    $receiver_name =$db->getOne($sql);

                    $res2 = array(
                        'sender_name' => $sender_name,
                        'receiver_name' => $receiver_name,
                        'message' => $v
                    );
                    $res1[] = $res2;

                }
                $res = array(
                    'type' => 2,
                    'sender_name' => $sender_name,
                    'receiver_name' => $receiver_name,
                    'message' => $value,
                    'second_hi_message' => $res1
                );
                $arr0[] = $res;
            }
        }$sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
        $sender_name =$db->getOne($sql);
        $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
        $receiver_name =$db->getOne($sql);
        $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE  receive_message_id = '".$value['id']."' ORDER BY send_time";
        $second_hi_message = $db->getAll($sql);
        foreach ($first_hi_message as $v){
            $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['sender_id'] . "' ";
            $sender_name =$db->getOne($sql);
            $sql = "SELECT user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['receiver_id'] . "' ";
            $receiver_name =$db->getOne($sql);

            $res2 = array(
                'sender_name' => $sender_name,
                'receiver_name' => $receiver_name,
                'message' => $v
            );
            $res1[] = $res2;

        }
        $res = array(
            'sender_name' => $sender_name,
            'receiver_name' => $receiver_name,
            'message' => $value,
            'second_hi_message' => $res1
        );
        $arr0[] = $res;
    }

//    $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['receiver_id'] ." ORDER BY send_time DESC";
//    $read0 = $db->getAll($sql);
//    foreach ($read0 as $value){
//        $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$value['sender_id'];
//        $sender_name = $db->getRow($sql);
//        $row0 = array(
//            'sender_name' => $sender_name,
//            'message' => $value
//        );
//        $arr0[] = $row0;
//    }

    $sql = "SELECT ecs_message.*,ecs_message_text.* FROM ".$ecs->table('message')."INNER JOIN " .$ecs->table('message_text')." ON ecs_message.message_text_id = ecs_message_text.message_text_id WHERE ecs_message.receiver_id = " .$_POST['receiver_id']. " ORDER BY send_time DESC";
    $read1 = $db->getAll($sql);
    foreach ($read1 as $value) {
        $sql = "SELECT user_name FROM " . $ecs->table('user') . " WHERE user_id = " . $value['sender_id'];
        $sender_name = $db->getRow($sql);
        $sql = "SELECT activity_name FROM " . $ecs->table('activity') . " WHERE activity_id = " . $value['activity_id'];
        $activity_name = $db->getRow($sql);
        $row1 = array(
            'sender_name' => $sender_name,
            'activity_name' => $activity_name,
            'message' => $value
        );
        $arr1[] = $row1;
    }

    $sql = "SELECT * FROM ".$ecs->table('inform_message')." WHERE receiver_id = " .$_POST['receiver_id']. " ORDER BY send_time DESC";
    $read2 = $db->getAll($sql);
    foreach ($read2 as $value) {
        $sql = "SELECT user_name FROM " . $ecs->table('user') . " WHERE user_id = " . $value['sender_id'];
        $sender_name = $db->getRow($sql);
        $sql = "SELECT activity_name FROM " . $ecs->table('activity') . " WHERE activity_id = " . $value['activity_id'];
        $activity_name = $db->getRow($sql);
        $row2 = array(
            'sender_name' => $sender_name,
            'activity_name' => $activity_name,
            'message' => $value
        );
        $arr2[] = $row2;
    }

    $message = array(
        'hi' => $arr0,
        'review' => $arr1,
        'inform' => $arr2
    );

    return Response::json(200,'消息获取成功',$message);
}

//更新未读状态接口
elseif($_REQUEST['act'] == 'change_state') {

    $_POST['message_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';
    $_POST['type'] = isset($_POST['type']) ? trim($_POST['type']) : '';

    if($_POST['type'] == 0){
        $sql = "UPDATE ".$ecs->table('message')." SET read_flag = 1 WHERE id = ".$_POST['message_id'];
        $db->query($sql);
        if (mysql_affected_rows()) {
            return Response::json(200,'状态修改成功');
        }else{
            return Response::json(500,'状态修改失败');
        }
    }elseif ($_POST['type'] == 1){
        $sql = "UPDATE ".$ecs->table('add_friend_message')."SET read_flag = 1 WHERE message_id = ".$_POST['message_id'];
        $db->query($sql);
        if (mysql_affected_rows()) {
            return Response::json(200,'状态修改成功');
        }else{
            return Response::json(500,'状态修改失败');
        }

    }elseif ($_POST['type'] == 2){
        $sql = "UPDATE ".$ecs->table('inform_message')."SET read_flag = 1 WHERE id = ".$_POST['message_id'];
        $db->query($sql);
        if (mysql_affected_rows()) {
            return Response::json(200,'状态修改成功');
        }else{
            return Response::json(500,'状态修改失败');
        }

    }
}


//回复打招呼消息接口
elseif($_REQUEST['act'] == 'reply_add_friend_message') {
    $_POST['sender_id'] = isset($_POST['sender_id']) ? trim($_POST['sender_id']) : '';
    $_POST['receiver_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';
    $_POST['receive_message_id'] = isset($_POST['receive_message_id']) ? trim($_POST['receive_message_id']) : '';
    $_POST['message_text'] = isset($_POST['message_text']) ? trim($_POST['message_text']) : '';
//    $_POST['sender_id'] = 2;
//    $_POST['receiver_id'] = 1;
//    $_POST['receive_message_id'] = 3;
//    $_POST['message_text'] = '5546465';



    $sql = "SELECT receive_message_id FROM ".$ecs->table('add_friend_message')." WHERE id = " .$_POST['receive_message_id'] ;
    $receive_message_id = $db->getOne($sql);
    if(!empty($receive_message_id)){
        $send_time = date("Y-m-d H:i:s");
        $sql = "insert into ". $ecs->table('add_friend_message'). " (sender_id,receiver_id,receive_message_id,send_time,message_text) values('" .$_POST['sender_id']."','".$_POST['receiver_id']."','".$receive_message_id."','".$send_time."','".$_POST['message_text']."')";
        $db->query($sql);
        if (mysql_affected_rows()) {
            //调用极光接口推送
            $pushObj = new Jpush();
            $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['receiver_id'] ;
            $message = $db->getRow($sql);
            $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$_POST['sender_id'] ;
            $sender_name = $db->getOne($sql);
            $push_content = array(
                'sender_name' => $sender_name,
                'message' => $message
            );
            $content = json_encode($push_content);
            $sql = "SELECT registration_id FROM ".$ecs->table('registration_id')." WHERE user_id = " .$_POST['receiver_id'] ;
            $registration_id = $db->getOne($sql);
            $receive = array('registration_id'=>array($registration_id));
            $result = $pushObj->push($receive,$content);
            if($result){
                $res_arr = json_decode($result, true);
                if(isset($res_arr['error'])) {                       //如果返回了error则证明失败
                    echo $res_arr['error']['message'];          //错误信息
                    echo $res_arr['error']['code'];             //错误码
                    return false;
                }
            }else{      //接口调用失败或无响应
                echo '推送接口调用失败或无响应';
                return false;
            }
            return Response::json(200,'回复成功');
        }else{
            return Response::json(500,'回复失败');
        }

    }else{
        $send_time = date("Y-m-d H:i:s");
        $sql = "insert into ". $ecs->table('add_friend_message'). " (sender_id,receiver_id,receive_message_id,send_time,message_text) values('" .$_POST['sender_id']."','".$_POST['receiver_id']."','".$_POST['receive_message_id']."','".$send_time."','".$_POST['message_text']."')";
        $db->query($sql);
        if (mysql_affected_rows()) {
            //调用极光接口推送
            $pushObj = new Jpush();
            $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['receiver_id'] ;
            $message = $db->getRow($sql);
            $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$_POST['sender_id'] ;
            $sender_name = $db->getOne($sql);
            $push_content = array(
                'sender_name' => $sender_name,
                'message' => $message
            );
            $content = json_encode($push_content);
            $sql = "SELECT registration_id FROM ".$ecs->table('registration_id')." WHERE user_id = " .$_POST['friend_id'] ;
            $registration_id = $db->getOne($sql);
            $receive = array('registration_id'=>array($registration_id));
            $result = $pushObj->push($receive,$content);
            if($result){
                $res_arr = json_decode($result, true);
                if(isset($res_arr['error'])) {                       //如果返回了error则证明失败
                    echo $res_arr['error']['message'];          //错误信息
                    echo $res_arr['error']['code'];             //错误码
                    return false;
                }
            }else{      //接口调用失败或无响应
                echo '推送接口调用失败或无响应';
                return false;
            }
            return Response::json(200,'回复成功');
        }else{
            return Response::json(500,'回复失败');
        }
    }


}


/**
 * Created by PhpStorm.
 * User: tuhao
 * Date: 2016/11/4
 * Time: 15:24
 */